Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: maximum call stack size exceeded at paths.js (findYarnLock function) #811

Conversation

CelsoDeCarvalho
Copy link
Contributor

I was facing this error when I run the d2 app scripts init pmtct to create a new app. Then I found this solution in the CoP: https://community.dhis2.org/t/rangeerror-maximum-call-stack-size-exceeded-error-when-i-run-d2-app-scripts-init-my-app/49023

But` there we needa delete findYarnLock function. Earlier today, when a was checking this function, I noticed that this function is in an infinite loop on windows systems because the base case inside this function is not verified only in windows.

dhis2 error

The line if(base==="/") I think we are checking only for mac or linux OS 'cause in windows, is if(base==="C:\\") .
Capture123


I tried this solution and it worked for me.

Capturesdsd

@amcgee amcgee changed the title fix:maximum call stack size exceeded at paths.js(findYarnLock function) fix: maximum call stack size exceeded at paths.js(findYarnLock function) Jun 27, 2023
@amcgee amcgee changed the title fix: maximum call stack size exceeded at paths.js(findYarnLock function) fix: maximum call stack size exceeded at paths.js (findYarnLock function) Jun 27, 2023
@amcgee
Copy link
Member

amcgee commented Jun 27, 2023

Thank you for this PR @CelsoDeCarvalho ! It generally looks good. Could you change it to use path.parse(process.cwd()).root on all operating systems instead of only on Windows? The same should work on *nix machines as well.

@CelsoDeCarvalho
Copy link
Contributor Author

CelsoDeCarvalho commented Jun 27, 2023

It is unnecessary to use path.parse(process.cwd()).root for non windows OS, because it is known that the root will be "/" I think. It will help us to avoid invoke parse and cwd function unnecessarly.

@CelsoDeCarvalho
Copy link
Contributor Author

Do you think that I needa change it?

@amcgee
Copy link
Member

amcgee commented Jun 29, 2023

Thanks Celso - thinking about this a little more, I think using process.cwd() actually might not always be correct. If a directory with another root (like D://) is passed as the base directory it will never be found. Also the way this change is currently implemented the root is computed recursively but will never change, so it would be a bit more efficient to only do this calculation once.

I would recommend instead changing this line to findYarnLock(base, path.parse(base).root) and then updating the findYarnLock function to accept both base and root as parameters, and passing both when calling it recursively.

Let me know if this makes sense to you, thanks again for helping to fix this!

@amcgee amcgee self-requested a review June 29, 2023 15:06
@amcgee
Copy link
Member

amcgee commented Aug 2, 2023

Hi @CelsoDeCarvalho thanks again for your contribution here! Do you think you will have time to apply the changes requested? I'd like to get this fixed, so if you don't have time I can also do it myself and merge a separate PR. Let me know!

@CelsoDeCarvalho
Copy link
Contributor Author

Thanks @amcgee for text me. Oh! I had forgotten about this point. I'll make changes today. Thanks

It will be a pleasure to contribute to this repo.

@CelsoDeCarvalho
Copy link
Contributor Author

Thanks Celso - thinking about this a little more, I think using process.cwd() actually might not always be correct. If a directory with another root (like D://) is passed as the base directory it will never be found. Also the way this change is currently implemented the root is computed recursively but will never change, so it would be a bit more efficient to only do this calculation once.

I would recommend instead changing this line to findYarnLock(base, path.parse(base).root) and then updating the findYarnLock function to accept both base and root as parameters, and passing both when calling it recursively.

Let me know if this makes sense to you, thanks again for helping to fix this!

@amcgee This make sense. The variable declaration inside a recursive function can decrease the perfomance. I have made the changes. But for the first point, can you please explain it?
Thanks.

Comment on lines 23 to 24
// Get the root path or drive based on the operating system
const rootPathByOS = path.parse(cwd).root;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is no longer dependent on the operating system, it should work for all operating systems, so we should remove this comment and change the name to simply rootDirectory

Suggested change
// Get the root path or drive based on the operating system
const rootPathByOS = path.parse(cwd).root;
const rootDirectory = path.parse(cwd).root;

@amcgee
Copy link
Member

amcgee commented Aug 3, 2023

@CelsoDeCarvalho thanks and certainly!

Basically, the variable cwd here will default to process.cwd() (the current working directory from which this command is run) but it can be overridden with a command line argument. So it would be possible to do this:

> cd D:\test\folder
> d2 app scripts init --cwd C:\development\apps mynewapp

In this case, process.cwd() would be on the D:\ drive but the cwd argument (which indicates where the new app should be created) would be on the C:\ drive. Since we're starting from the cwd directory and traversing up the file system heirarchy to find the root directory, we'd eventually get to C:\. If we used path.parse(process.cwd()).root instead of path.parse(base).root it would try to match D:\ as the eventual base condition to terminate the recursion, but that would never happen. So we need to make sure that we're finding the root directory of the directory we're working on which might not be the current working directory in bash. Does that make sense?

This change looks good to me now! I made one very minor suggestion to update the naming of the root variable. Thanks again for your contribution!

@amcgee
Copy link
Member

amcgee commented Aug 3, 2023

Don't worry about the commit message lint issue - it's caused by the fact that several commits here aren't exactly following conventional commit standards - 6b61c07 is missing a : and 201b22a is missing a (space) after the :.

I will squash-merge this branch when I merge it, so no need to fix those on your branch :-)

@CelsoDeCarvalho
Copy link
Contributor Author

CelsoDeCarvalho commented Aug 6, 2023

@CelsoDeCarvalho thanks and certainly!

Basically, the variable cwd here will default to process.cwd() (the current working directory from which this command is run) but it can be overridden with a command line argument. So it would be possible to do this:

> cd D:\test\folder
> d2 app scripts init --cwd C:\development\apps mynewapp

In this case, process.cwd() would be on the D:\ drive but the cwd argument (which indicates where the new app should be created) would be on the C:\ drive. Since we're starting from the cwd directory and traversing up the file system heirarchy to find the root directory, we'd eventually get to C:\. If we used path.parse(process.cwd()).root instead of path.parse(base).root it would try to match D:\ as the eventual base condition to terminate the recursion, but that would never happen. So we need to make sure that we're finding the root directory of the directory we're working on which might not be the current working directory in bash. Does that make sense?

This change looks good to me now! I made one very minor suggestion to update the naming of the root variable. Thanks again for your contribution!

Hi @amcgee

So Lemme see if I get it. Basically you saying that there will be possible to create app in a directory passed through the command line in --cwd flag and some times if the directory am passing be diferent of my current working directory, the recursion stop condition will not be verified, rite? And we have done that point 'cause we're using cwd variable that I think this is the one that will be used as the command line argument. Please let me know if am right. Thanks

I have made the changes.

@amcgee
Copy link
Member

amcgee commented Aug 21, 2023

@CelsoDeCarvalho thanks and certainly!
Basically, the variable cwd here will default to process.cwd() (the current working directory from which this command is run) but it can be overridden with a command line argument. So it would be possible to do this:

> cd D:\test\folder
> d2 app scripts init --cwd C:\development\apps mynewapp

In this case, process.cwd() would be on the D:\ drive but the cwd argument (which indicates where the new app should be created) would be on the C:\ drive. Since we're starting from the cwd directory and traversing up the file system heirarchy to find the root directory, we'd eventually get to C:\. If we used path.parse(process.cwd()).root instead of path.parse(base).root it would try to match D:\ as the eventual base condition to terminate the recursion, but that would never happen. So we need to make sure that we're finding the root directory of the directory we're working on which might not be the current working directory in bash. Does that make sense?
This change looks good to me now! I made one very minor suggestion to update the naming of the root variable. Thanks again for your contribution!

Hi @amcgee

So Lemme see if I get it. Basically you saying that there will be possible to create app in a directory passed through the command line in --cwd flag and some times if the directory am passing be diferent of my current working directory, the recursion stop condition will not be verified, rite? And we have done that point 'cause we're using cwd variable that I think this is the one that will be used as the command line argument. Please let me know if am right. Thanks

I have made the changes.

That's correct, thank you @CelsoDeCarvalho !

@amcgee amcgee enabled auto-merge (squash) August 21, 2023 17:31
@amcgee amcgee disabled auto-merge August 21, 2023 17:39
@amcgee amcgee merged commit 22a6863 into dhis2:master Aug 21, 2023
1 of 2 checks passed
@amcgee
Copy link
Member

amcgee commented Aug 21, 2023

I manually built, tested, and linted this PR before merging. Thanks again @CelsoDeCarvalho for the contribution!!

dhis2-bot added a commit that referenced this pull request Aug 21, 2023
## [10.3.10](v10.3.9...v10.3.10) (2023-08-21)

### Bug Fixes

* support yarn.lock discovery on non-unix ([#811](#811)) ([22a6863](22a6863))
@dhis2-bot
Copy link
Contributor

🎉 This PR is included in version 10.3.10 🎉

The release is available on:

Your semantic-release bot 📦🚀

dhis2-bot added a commit that referenced this pull request Aug 22, 2023
# [10.4.0-alpha.4](v10.4.0-alpha.3...v10.4.0-alpha.4) (2023-08-22)

### Bug Fixes

* support yarn.lock discovery on non-unix ([#811](#811)) ([22a6863](22a6863))
@dhis2-bot
Copy link
Contributor

🎉 This PR is included in version 10.4.0-alpha.4 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

Successfully merging this pull request may close these issues.

None yet

3 participants